﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NPOI.SS.Formula.Functions;
using OSSUtility.Device;

namespace Ozone.Device
{

    /// <summary>
    /// 流量计
    /// </summary>
    public class DevFlowmeter : DevBase
    {
        public DevFlowmeter(int CommType, string FSrvIP, int FPort) : base(CommType, FSrvIP, FPort)
        {

        }


        #region 流量计
        /// <summary>
        /// 打开且设置流量计流速  通风橱2流量计 流量计量程 1000
        /// </summary>
        /// <param name="flow"></param>
        /// <returns></returns>
        public bool OpenAndSetFlowmeter(int flow)
        {
            string Message = string.Empty;
            bool CloseFlowmeterState = false;

            try
            {
                //流速转换
                string Strflow = "";
                flow = Convert.ToInt32((Convert.ToDouble(flow) / 1000) * 5000);
                Strflow = flow.ToString("D4");
                //发→P6,SA,1000,10
                //收←P6,OK
                string command = "P6,SA," + Strflow + ",10\r\n";
                string recvStr = session_.sendAndReadStrLock(command, 200);

                if (recvStr.Contains("OK"))
                {
                    CloseFlowmeterState = true;
                }
                else
                {
                    CloseFlowmeterState = false;
                }
            }
            catch (Exception ex)
            {
                CloseFlowmeterState = false;
                Message = ex.Message;
            }


            return CloseFlowmeterState;
        }


        /// <summary>
        /// 关闭流量计
        /// </summary>
        /// <param name="Temperature"></param>
        /// <returns></returns>
        public bool CloseFlowmeter()
        {
            string Message = string.Empty;
            bool CloseFlowmeterState = false;

            try
            {

                //发→P6,SA,0000,00
                //收←P6,OK
                string command = "P6,SA,0000,00\r\n";
                string recvStr = session_.sendAndReadStrLock(command, 200);

                if (recvStr.Contains("OK"))
                {
                    CloseFlowmeterState = true;
                }
                else
                {
                    CloseFlowmeterState = false;
                }
            }
            catch (Exception ex)
            {
                CloseFlowmeterState = false;
                Message = ex.Message;
            }

            return CloseFlowmeterState;
        }


        /// <summary>
        /// 获取当前流量计流速
        /// </summary>
        /// <returns></returns>
        public double GetNowDevFlowmeter()
        {

            string Message = string.Empty;
            double NowDevFlowmeter = 0;


            try
            {
                string command = "P6,QA\r\n";
                string recvStr = session_.sendAndReadStrLock(command, 500);

                if (recvStr.Contains("P6,Qa"))
                {
                    string[] ArryrecvStr = recvStr.Split(',');
                    int FlowValue = Convert.ToInt32(ArryrecvStr[2]);
                    double Value = (FlowValue * 1000) / 5000;
                    NowDevFlowmeter = Value;
                }
                else
                {
                    NowDevFlowmeter = 0;
                }
            }
            catch (Exception ex)
            {
                NowDevFlowmeter = 0;
                Message = ex.Message;
            }


            return NowDevFlowmeter;
        }

        #endregion






        #region 流量计第二路  通风橱1氮吹流量计
        /// <summary>
        /// 打开且设置流量计流速  （流量计为 100的流速）
        /// </summary>
        /// <param name="flow"></param>
        /// <returns></returns>
        public bool OpenAndSetFlowmeter2(int flow)
        {
            string Message = string.Empty;
            bool CloseFlowmeterState = false;

            try
            {
                //流速转换
                string Strflow = "";
                flow = Convert.ToInt32((Convert.ToDouble(flow) / 100) * 5000);
                //补齐4位
                Strflow = flow.ToString("D4");
                //发→P6,SB,1000,10
                //收←P6,OK
                string command = "P6,SB," + Strflow + ",10\r\n";
                string recvStr = session_.sendAndReadStrLock(command, 500);

                if (recvStr.Contains("OK"))
                {
                    CloseFlowmeterState = true;
                }
                else
                {
                    CloseFlowmeterState = false;
                }
            }
            catch (Exception ex)
            {
                CloseFlowmeterState = false;
                Message = ex.Message;
            }


            return CloseFlowmeterState;
        }


        /// <summary>
        /// 关闭流量计
        /// </summary>
        /// <param name="Temperature"></param>
        /// <returns></returns>
        public bool CloseFlowmeter2()
        {
            string Message = string.Empty;
            bool CloseFlowmeterState = false;

            try
            {

                //发→P6,SB,0000,00
                //收←P6,OK
                string command = "P6,SB,0000,00\r\n";
                string recvStr = session_.sendAndReadStrLock(command, 200);

                if (recvStr.Contains("OK"))
                {
                    CloseFlowmeterState = true;
                }
                else
                {
                    CloseFlowmeterState = false;
                }
            }
            catch (Exception ex)
            {
                CloseFlowmeterState = false;
                Message = ex.Message;
            }

            return CloseFlowmeterState;
        }


        /// <summary>
        /// 获取当前流量计流速
        /// </summary>
        /// <returns></returns>
        public double GetNowDevFlowmeter2()
        {

            string Message = string.Empty;
            double NowDevFlowmeter = 0;


            try
            {
                string command = "P6,QB\r\n";
                string recvStr = session_.sendAndReadStrLock(command, 500);

                if (recvStr.Contains("P6,Qb"))
                {
                    string[] ArryrecvStr = recvStr.Split(',');
                    int FlowValue = Convert.ToInt32(ArryrecvStr[2]);
                    double Value = (FlowValue * 100) / 5000;
                    NowDevFlowmeter = Value;
                }
                else
                {
                    NowDevFlowmeter = 0;
                }
            }
            catch (Exception ex)
            {
                NowDevFlowmeter = 0;
                Message = ex.Message;
            }


            return NowDevFlowmeter;
        }

        #endregion


    }
}
